// pages/userCenter/share/share.js
const request = require('../../../utils/request')

Page({
  data: {
    inviteCode: '',
    shareList: []
  },

  onLoad(options) {
    this.getShareList()
    const token = wx.getStorageSync("token")
    if (!token && options.inviteCode) {
      wx.redirectTo({
        url: `/pages/index/login/regMini/regMini?inviteCode=${options.inviteCode}`
      })
      return
    }
    this.getMerberInfo()
  },

  onShareAppMessage() {
    return {
      title: '车知友',
      path: `/pages/userCenter/share/share?inviteCode=${this.data.inviteCode}`
    }
  },

  getMerberInfo() {
    wx.showLoading()
    request.get('member_info')
      .then(res => {
        wx.hideLoading()
        this.setData({
          inviteCode: res.data.invite
        })
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        
      })
  },

  getShareList() {
    wx.showLoading()
    request.get('my_xiajia')
      .then(res => {
        wx.hideLoading()
        if (res.data && res.data.length) {
          this.setData({
            shareList: res.data
          })
        }
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        
      })
  }
})